package ed.hbut.supermarket1.controller;


import ed.hbut.supermarket1.entity.Category;
import ed.hbut.supermarket1.entity.Result;
import ed.hbut.supermarket1.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class CategoryController {


    @Autowired
    public CategoryService categoryService;

    /**
     * 查询所有分类
     * @return
     */
    @GetMapping("/api/category")
    public Result getAllCategory() {
        log.info("获取类型");
         List<Category> list=categoryService.getAllCategory();
        return Result.success(list);
    }

    @PostMapping("/api/category")
    public Result addCategory(@RequestBody Category category) {

        System.out.println(category);
        categoryService.addCategory(category);
        return Result.success();
    }

    @PutMapping("/api/category")
    public Result updateCategory(@RequestBody Category category) {
        System.out.println(category);
        categoryService.updateCategory(category);
        return Result.success();
    }

    @DeleteMapping("/api/category/{id}")
    public Result deleteCategory(@PathVariable Integer id) {


        log.info("传过来要删除的分类id"+id.toString());
        categoryService.delete(id);

        return Result.success();
    }

}
